package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.domain.Order;
import com.itheima.domain.Setmeal;
import com.itheima.entity.PageResult;
import com.itheima.entity.Result;
import com.itheima.service.OrderService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Reference
    private OrderService orderService;

    @RequestMapping("/findSetmeal")
    public Result findSetmeal() {
        try {
            List<Setmeal> list = orderService.findSetmeal();
            return new Result(true, MessageConstant.QUERY_SETMEALLIST_SUCCESS, list);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_SETMEALLIST_FAIL);
        }
    }

    /*完成电话预约*/
    @RequestMapping("/save")
    public Result save(Integer setmealIds, @RequestBody Map map) {
        /*设置预约类型为电话预约*/
        map.put("orderType", Order.ORDERTYPE_TELEPHONE);
        Result result = null;
        try {
            result = orderService.result(setmealIds, map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }

    /*分页查询*/
    @RequestMapping("/findPage")
    public PageResult findPage(String param, @RequestBody Map map) throws Exception {
        PageResult pageResult = orderService.findPage(param, map);
        return pageResult;
    }

    /*取消预约*/
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {

            orderService.delete(id);
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_ORDER_FAIL);
        }
    }

    /*更改预约状态*/
    @RequestMapping("/updateStatus")
    public Result deleteStatus(Integer id) {
        try {
            orderService.updateStatus(id);
            return new Result(true, MessageConstant.DELETE_ORDER_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "修改失败！");
        }
    }

    /*根据id查询预约的基本数据*/
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Map map = orderService.findOrderById(id);
            return new Result(true, MessageConstant.FIND_ORDER_SUCCESS, map);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.FIND_ORDER_FAIL);
        }
    }

    @RequestMapping("/findSetmealId")
    public Result find(Integer id) throws Exception {
        try {
            List<Integer> setmealId = orderService.findSetmealId(id);
            return new Result(true, MessageConstant.QUERY_ORDER_SUCCESS, setmealId);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.QUERY_ORDER_FAIL);
        }
    }

    /*更新预约数据*/
    @RequestMapping("/update")
    public Result resultByUpdate(Integer setmealIds, @RequestBody Map map) {
        Result result=null;
        try {
            result = orderService.resultByUpdate(setmealIds, map);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
            return result;
        }
    }
}
